package top.ed333.mcplugins.configurableevents.action;

import me.clip.placeholderapi.PlaceholderAPI;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import top.ed333.mcplugins.configurableevents.DependenciesResolver;

import java.util.Collection;

public class BroadCastAction extends Action {
    public BroadCastAction(Object value) {
        super(value);
    }

    @Override
    public String name() {
        return "broadcast";
    }

    @Override
    public void execute(@Nullable Player player) {
        if (value() instanceof Collection) {
            ((Collection<?>) value()).forEach(val -> {
                String msg = DependenciesResolver.isPapiInstalled() ? PlaceholderAPI.setPlaceholders(player, val.toString()) : val.toString();
                Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&',  msg));
            });
        } else {
            String msg = DependenciesResolver.isPapiInstalled() ? PlaceholderAPI.setPlaceholders(player, value().toString()) : value().toString();
            Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&',  msg));
        }
    }
}
